package org.zero.concurrency.demo.example.immutable;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import lombok.extern.slf4j.Slf4j;
import org.zero.concurrency.demo.annotations.ThreadSafe;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @program: concurrency-demo
 * @description:
 * @author: 01
 * @create: 2018-10-17 10:37
 **/
@Slf4j
@ThreadSafe
public class ImmutableExample3 {
    /**
     * 不可变的list
     */
    private final static List<Integer> list = ImmutableList.of(1, 2, 3);

    /**
     * 不可变的set
     */
    private final static Set<Integer> set = ImmutableSet.copyOf(list);

    /**
     * 不可变的map，需要以k/v的形式传入数据，即奇数位参数为key，偶数位参数为value
     */
    private final static Map<Integer, Integer> map = ImmutableMap.of(1, 1, 2, 2, 3, 3);

    /**
     * 通过builder调用链的方式构造不可变的map
     */
    private final static Map<Integer, Integer> map2 = ImmutableMap.<Integer, Integer>builder()
            .put(1, 1).put(2, 2).put(3, 3).build();

    public static void main(String[] args) {
        // 修改对象内的数据就会抛出UnsupportedOperationException异常
        list.add(4);
        set.add(5);
        map.put(1, 2);
        map2.put(1, 2);
    }
}
